import { parse } from 'url';
import webpath from '@/utils/webpath';

// mock tableListDataSource
let tableListDataSource = [];
for (let i = 0; i < 46; i += 1) {
    tableListDataSource.push({
        id: `李海波 id ${i}`,
        article_title: `李海波 article_title ${i}`,
        article_class_id: `李海波 article_class_id ${i}`,
        article_origin: `李海波 article_origin ${i}`,
        article_origin_address: `李海波 article_origin_address ${i}`,
        article_author: `李海波 article_author ${i}`,
        article_abstract: `李海波 article_abstract ${i}`,
        article_content: `李海波 article_content ${i}`,
        article_image: `李海波 article_image ${i}`,
        article_keyword: `李海波 article_keyword ${i}`,
        article_link: `李海波 article_link ${i}`,
        article_goods: `李海波 article_goods ${i}`,
        article_start_time: `李海波 article_start_time ${i}`,
        article_end_time: `李海波 article_end_time ${i}`,
        article_publish_time: `李海波 article_publish_time ${i}`,
        article_click: `李海波 article_click ${i}`,
        article_sort: `李海波 article_sort ${i}`,
        article_commend_flag: `李海波 article_commend_flag ${i}`,
        article_comment_flag: `李海波 article_comment_flag ${i}`,
        article_verify_admin: `李海波 article_verify_admin ${i}`,
        article_verify_time: `李海波 article_verify_time ${i}`,
        article_state: `李海波 article_state ${i}`,
        article_publisher_name: `李海波 article_publisher_name ${i}`,
        article_publisher_id: `李海波 article_publisher_id ${i}`,
        article_type: `李海波 article_type ${i}`,
        article_attachment_path: `李海波 article_attachment_path ${i}`,
        article_image_all: `李海波 article_image_all ${i}`,
        article_modify_time: `李海波 article_modify_time ${i}`,
        article_tag: `李海波 article_tag ${i}`,
        article_comment_count: `李海波 article_comment_count ${i}`,
        article_attitude_1: `李海波 article_attitude_1 ${i}`,
        article_attitude_2: `李海波 article_attitude_2 ${i}`,
        article_attitude_3: `李海波 article_attitude_3 ${i}`,
        article_attitude_4: `李海波 article_attitude_4 ${i}`,
        article_attitude_5: `李海波 article_attitude_5 ${i}`,
        article_attitude_6: `李海波 article_attitude_6 ${i}`,
        article_title_short: `李海波 article_title_short ${i}`,
        article_attitude_flag: `李海波 article_attitude_flag ${i}`,
        article_commend_image_flag: `李海波 article_commend_image_flag ${i}`,
        article_share_count: `李海波 article_share_count ${i}`,
        article_verify_reason: `李海波 article_verify_reason ${i}`,
        article_class_name: `李海波 article_class_name ${i}`,
        article_display: `李海波 article_display ${i}`,
        article_agree_count: `李海波 article_agree_count ${i}`,
        article_title_link: `李海波 article_title_link ${i}`,
        status: Math.floor(Math.random() * 10) % 4,
    });
}

function getEbookArticle(req, res, u) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const params = parse(url, true).query;

    let dataSource = tableListDataSource;

    if (params.sorter!=undefined) {
        const s = params.sorter.split('_');
        dataSource = dataSource.sort((prev, next) => {
            if (s[1] === 'descend') {
            return next[s[0]] - prev[s[0]];
        }
        return prev[s[0]] - next[s[0]];
    });
    }

    if (params.status!=undefined) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
    filterDataSource = filterDataSource.concat(
    dataSource.filter(data =>
    {

        if(data.status!=undefined){
        return parseInt(data.status, 10) === parseInt(s[0], 10);
        }
    })
    );
    });
    dataSource = filterDataSource;
    }

    if (params.name!=undefined) {
    dataSource = dataSource.filter(data => {

        if(data.name!=undefined){
        return data.name.indexOf(params.name) > -1;
        }

    });
    }

    let pageSize = 10;
    if (params.pageSize!=undefined) {
        pageSize = params.pageSize * 1;
    }

    const result = {
        list: dataSource,
        pagination: {
            total: dataSource.length,
            pageSize,
            current: parseInt(params.currentPage, 10) || 1,
        },
    };

    return res.json(result);
}



function deleteByPrimaryKeyForEbookArticle(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;

    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getEbookArticle(req, res, u);
}


function selectByPrimaryKeyForEbookArticle(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getEbookArticle(req, res, u);
}



function insertForEbookArticle(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;



    const i = Math.ceil(Math.random() * 10000);
    tableListDataSource.unshift({
        id: `李海波 id ${i}`,
        article_title: `李海波 article_title ${i}`,
        article_class_id: `李海波 article_class_id ${i}`,
        article_origin: `李海波 article_origin ${i}`,
        article_origin_address: `李海波 article_origin_address ${i}`,
        article_author: `李海波 article_author ${i}`,
        article_abstract: `李海波 article_abstract ${i}`,
        article_content: `李海波 article_content ${i}`,
        article_image: `李海波 article_image ${i}`,
        article_keyword: `李海波 article_keyword ${i}`,
        article_link: `李海波 article_link ${i}`,
        article_goods: `李海波 article_goods ${i}`,
        article_start_time: `李海波 article_start_time ${i}`,
        article_end_time: `李海波 article_end_time ${i}`,
        article_publish_time: `李海波 article_publish_time ${i}`,
        article_click: `李海波 article_click ${i}`,
        article_sort: `李海波 article_sort ${i}`,
        article_commend_flag: `李海波 article_commend_flag ${i}`,
        article_comment_flag: `李海波 article_comment_flag ${i}`,
        article_verify_admin: `李海波 article_verify_admin ${i}`,
        article_verify_time: `李海波 article_verify_time ${i}`,
        article_state: `李海波 article_state ${i}`,
        article_publisher_name: `李海波 article_publisher_name ${i}`,
        article_publisher_id: `李海波 article_publisher_id ${i}`,
        article_type: `李海波 article_type ${i}`,
        article_attachment_path: `李海波 article_attachment_path ${i}`,
        article_image_all: `李海波 article_image_all ${i}`,
        article_modify_time: `李海波 article_modify_time ${i}`,
        article_tag: `李海波 article_tag ${i}`,
        article_comment_count: `李海波 article_comment_count ${i}`,
        article_attitude_1: `李海波 article_attitude_1 ${i}`,
        article_attitude_2: `李海波 article_attitude_2 ${i}`,
        article_attitude_3: `李海波 article_attitude_3 ${i}`,
        article_attitude_4: `李海波 article_attitude_4 ${i}`,
        article_attitude_5: `李海波 article_attitude_5 ${i}`,
        article_attitude_6: `李海波 article_attitude_6 ${i}`,
        article_title_short: `李海波 article_title_short ${i}`,
        article_attitude_flag: `李海波 article_attitude_flag ${i}`,
        article_commend_image_flag: `李海波 article_commend_image_flag ${i}`,
        article_share_count: `李海波 article_share_count ${i}`,
        article_verify_reason: `李海波 article_verify_reason ${i}`,
        article_class_name: `李海波 article_class_name ${i}`,
        article_display: `李海波 article_display ${i}`,
        article_agree_count: `李海波 article_agree_count ${i}`,
        article_title_link: `李海波 article_title_link ${i}`,
        status: Math.floor(Math.random() * 10) % 4,
    });


    return getEbookArticle(req, res, u);
}




function updateForEbookArticle(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;

    if(id!=undefined)
    tableListDataSource = tableListDataSource.map(item => {
        if (item.id === id) {
        Object.assign(item, { desc, name });
        return item;
    }
    return item;
});


    return getEbookArticle(req, res, u);
}


function deleteByMapForEbookArticle(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getEbookArticle(req, res, u);
}



function updateByMapForEbookArticle(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getEbookArticle(req, res, u);
}

function getOneForEbookArticle(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(undefined!=id)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getEbookArticle(req, res, u);
}

function getAllForEbookArticle(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getEbookArticle(req, res, u);
}

function queryPageForEbookArticle(req, res, u, b) {


    return getEbookArticle(req, res, u);
}

function countForEbookArticle(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getEbookArticle(req, res, u);
}


function insertBatchForEbookArticle(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getEbookArticle(req, res, u);
}



function deleteBatchForEbookArticle(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getEbookArticle(req, res, u);
}


function updateBatchForEbookArticle(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getEbookArticle(req, res, u);
}




export default {
    'GET /${webpath}/web/api/service/ebookarticle/delete': deleteByPrimaryKeyForEbookArticle,
    'GET /${webpath}/web/api/service/ebookarticle/get': selectByPrimaryKeyForEbookArticle,
    'POST /${webpath}/web/api/service/ebookarticle/insert': insertForEbookArticle,
    'POST /${webpath}/web/api/service/ebookarticle/update': updateForEbookArticle,
    'POST /${webpath}/web/api/service/ebookarticle/deleteByMap': deleteByMapForEbookArticle,
    'POST /${webpath}/web/api/service/ebookarticle/updateByMap': updateByMapForEbookArticle,
    'POST /${webpath}/web/api/service/ebookarticle/getOne': getOneForEbookArticle,
    'POST /${webpath}/web/api/service/ebookarticle/getAll': getAllForEbookArticle,
    'POST /${webpath}/web/api/service/ebookarticle/queryPage': queryPageForEbookArticle,
    'POST /${webpath}/web/api/service/ebookarticle/count': countForEbookArticle,
    'POST /${webpath}/web/api/service/ebookarticle/insertBatch': insertBatchForEbookArticle,
    'POST /${webpath}/web/api/service/ebookarticle/deleteBatch': deleteBatchForEbookArticle,
    'POST /${webpath}/web/api/service/ebookarticle/updateBatch': updateBatchForEbookArticle,
};
